home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 8 / FM Towns Free Software Collection 8.iso / t_os / tifsel / tifsel.c next >
Text File  |  1994-06-01  |  11KB  |  485 lines

  1. /********************************************************************
  2. *    TownsMenu 背景画選択プログラム                                        *
  3. ********************************************************************/
  4.  
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <dos.h>
  8. #include    <string.h>
  9. #include    <tifflib.h>
  10. #include    <egb.h>
  11. #include    <msdos.cf>
  12. #include    <conio.h>
  13.  
  14.  
  15. #define        FILEMAX        (1000)
  16. #define        FILEBUF        (8*1024)
  17. #define        GRPHBUF        (150*1024)
  18. #define        PALTBUF        (16*8+4)
  19. #define        COMPBUF        (_max( DECOMP_WORK_SIZE, COMP_WORK_SIZE ))
  20.  
  21. FILE    *fp;
  22. char    *gwork;
  23. char    *fname;
  24. char    *sfile;
  25. char    *dfile;
  26. char    *fbp;
  27. char    *gbp;
  28. char    *pbp;
  29. char    *cbp;
  30. int        bpp;
  31. int        gx;
  32. int        gy;
  33. int        cmp;
  34. int        fil;
  35. long    str;
  36. long    clt;
  37. int        dx;
  38. int        dy;
  39. int        fsize;
  40.  
  41.  
  42. /****************************************************************
  43. *    ヘルプ画面表示                                                *
  44. ****************************************************************/
  45. void    help( void )    {
  46.     printf("************************************************************\n");
  47.     printf("*  TownsMenu 背景画選択プログラム (TIFSEL-V1.0L10  by BUN)    *\n");
  48.     printf("*                                                          *\n");
  49.     printf("*   (1)run386 tifsel -S[num] *.tif                         *\n");
  50.     printf("*      *.tifから背景画を選択する                           *\n");
  51.     printf("*      (numで指定した数のファイルから選択)                     *\n");
  52.     printf("*      (ただし、ファイル数は最大1000とする)                    *\n");
  53.     printf("*                                                          *\n");
  54.     printf("*   (2)run386 tifsel -C TIFFFILE.TIF                       *\n");
  55.     printf("*      TIFFFILE.TIFを同ファイル名で圧縮保存する                *\n");
  56.     printf("*                                                          *\n");
  57.     printf("*   (3)run386 tifsel -G TIFFFILE.TIF [pg] [md]             *\n");
  58.     printf("*      画面表示している画像をTIFFFILE.TIFに圧縮保存する    *\n");
  59.     printf("*      pg:ペ-ジ番号(省略時0), md:画面モ-ド(省略時1)         *\n");
  60.     printf("*                                                          *\n");
  61.     printf("*   (4)run386 tifsel -D TIFFFILE.TIF                       *\n");
  62.     printf("*      TIFFFILE.TIFを画面に表示する                        *\n");
  63.     printf("*                                                          *\n");
  64.     printf("*                                                          *\n");
  65.     printf("*            << 何かキ-を押してください! >>                *\n");
  66.     printf("************************************************************\n");
  67.  
  68.     while( _kbhit()==0 );
  69.     exit( -1 );
  70. }
  71.  
  72.  
  73. /****************************************************************
  74. *    メモリ不足                                                        *
  75. ****************************************************************/
  76. void    memerr( void )    {
  77.     printf("メモリ不足です!! \n");
  78.     exit( -1 );
  79. }
  80.  
  81.  
  82. /****************************************************************
  83. *    画面へデ-タを表示する                                        *
  84. ****************************************************************/
  85. int        disp_data( void )    {
  86.     char    para[64];
  87.     int        sx;
  88.     int        sy;
  89.  
  90.     EGB_init( gwork, EgbWorkSize );
  91.     if( bpp==1 || bpp==4 )    {
  92.         EGB_resolution( gwork, 0, 3 );
  93.         EGB_color( gwork, 0,  0 );
  94.         EGB_color( gwork, 1, 15 );
  95.         if( clt ) EGB_palette( gwork, 1, pbp );
  96.  
  97.         sx=(640-gx)/2;
  98.         sy=(480-gy)/2;
  99.     }
  100.     else if( bpp==16 )    {
  101.         EGB_resolution( gwork, 0, 10 );
  102.         EGB_displayStart( gwork, 3, 0, 0 );
  103.         EGB_displayStart( gwork, 0, 0, 0 );
  104.         EGB_displayStart( gwork, 1, 0, 0 );
  105.         EGB_displayStart( gwork, 2, 2, 2 );
  106.         EGB_displayStart( gwork, 3, 320, 240 );
  107.  
  108.         sx=(320-gx)/2;
  109.         sy=(240-gy)/2;
  110.     }
  111.     else    {
  112.         return( -1 );
  113.     }
  114.  
  115.     DWORD( para+ 0 )=(long)gbp;
  116.     WORD ( para+ 4 )=getds();
  117.     WORD ( para+ 6 )=sx;
  118.     WORD ( para+ 8 )=sy;
  119.     WORD ( para+10 )=sx+gx-1;
  120.     WORD ( para+12 )=sy+gy-1;
  121.     if( bpp!=1 )    {
  122.         EGB_putBlock( gwork, 0, para );
  123.     }
  124.     else    {
  125.         EGB_clearScreen( gwork );
  126.         EGB_putBlockColor( gwork, 0, para );
  127.     }
  128.  
  129.     return( 0 );
  130. }
  131.  
  132.  
  133. /****************************************************************
  134. *    画面からデ-タをロ-ドする                                        *
  135. ****************************************************************/
  136. int        load_scrn( int pg, int md )    {
  137.     char    para[64];
  138.  
  139.     EGB_getModeInfo( md, NULL, NULL, &gx, &gy, &bpp );
  140.     if( bpp==2 )    {
  141.         bpp=1;
  142.     }
  143.     else if( bpp==16 )    {
  144.         bpp=4;
  145.     }
  146.     else if( bpp==32768 )    {
  147.         bpp=16;
  148.     }
  149.     else    {
  150.         return( -1 );
  151.     }
  152.  
  153.     EGB_getPalette( pg, pbp );
  154.     clt=1;
  155.  
  156.     EGB_resolution( gwork, pg, md );
  157.     EGB_writePage( gwork, pg );
  158.  
  159.     DWORD( para+ 0 )=(long)gbp;
  160.     WORD ( para+ 4 )=getds();
  161.     WORD ( para+ 6 )=0;
  162.     WORD ( para+ 8 )=0;
  163.     WORD ( para+10 )=gx-1;
  164.     WORD ( para+12 )=gy-1;
  165.     EGB_getBlock( gwork, para );
  166.  
  167.     return( 0 );
  168. }
  169.  
  170.  
  171. /****************************************************************
  172. *    ファイルからデ-タをロ-ドする                                        *
  173. ****************************************************************/
  174. int        put_data( char *bp, int lofs, int line )    {
  175.     if( lofs<gy )    {
  176.         memcpy( gbp+lofs*dx*bpp/8, bp, line*dx*bpp/8 );
  177.         return( 0 );
  178.     }
  179.     else    {
  180.         return( -1 );
  181.     }
  182. }
  183.  
  184. int        get_file( char *bp, long size )    {
  185.     if( fread( bp, 1, size, fp ) == size )    {
  186.         return( 0 );
  187.     }
  188.     else    {
  189.         return( -1 );
  190.     }
  191. }
  192.  
  193. int        load_file( char *file )    {
  194.     if( (fp=fopen( file,"rb" ))==NULL ) return(-1);
  195.  
  196.     fread( fbp, 1, FILEBUF, fp );
  197.     if( TIFF_getHead( fbp, FILEBUF )<0 ) return(-1);
  198.  
  199.     bpp=TIFF_checkMode( &gx, &gy, &cmp, &fil, &str, &clt );
  200.     if( bpp==1 || bpp==4 )    {
  201.         if( gx>640 || gy>480 ) return(-1);
  202.         dx=(int)((gx+7)/8)*8;
  203.         dy=GRPHBUF/(dx*bpp/8);
  204.     }
  205.     else if( bpp==16 )    {
  206.         if( gx>320 || gy>240 ) return(-1);
  207.         dx=gx;
  208.         dy=GRPHBUF/(dx*bpp/8);
  209.     }
  210.     else    {
  211.         return( -1 );
  212.     }
  213.  
  214.     if( clt ) TIFF_getPal( pbp );
  215.  
  216.     TIFF_setLoadFunc( put_data, get_file );
  217.     TIFF_loadImage( bpp, gx, gy, str, fil, cmp, gbp, dx, dy, cbp );
  218.  
  219.     fclose( fp );
  220.     return( 0 );
  221. }
  222.  
  223.  
  224. /****************************************************************
  225. *    ファイルへデ-タをセ-ブする                                        *
  226. ****************************************************************/
  227. int        put_file( char *bp, long size )    {
  228.     if( fwrite( bp, 1, size, fp ) == size )    {
  229.         fsize += size;
  230.         return( 0 );
  231.     }
  232.     else    {
  233.         return( -1 );
  234.     }
  235. }
  236.  
  237. int        get_data( char *bp, int lofs, int line )    {
  238.     if( lofs<gy )    {
  239.         memcpy( bp, gbp+lofs*dx*bpp/8, line*dx*bpp/8 );
  240.         return( 0 );
  241.     }
  242.     else    {
  243.         return( -1 );
  244.     }
  245. }
  246.  
  247. int        save_file( char *file, int c )    {
  248.     int        size;
  249.     char    *pbpw;
  250.  
  251.     if( bpp==1 || bpp==4 )    {
  252.         if( gx>640 || gy>480 ) return(-1);
  253.         dx=(int)((gx+7)/8)*8;
  254.         dy=FILEBUF/(dx*bpp/8);
  255.     }
  256.     else if( bpp==16 )    {
  257.         if( gx>320 || gy>240 ) return(-1);
  258.         dx=gx;
  259.         dy=FILEBUF/(dx*bpp/8);
  260.     }
  261.     else    {
  262.         return( -1 );
  263.     }
  264.  
  265.     if( (fp=fopen( file,"wb" ))==NULL ) return(-1);
  266.  
  267.     size=TIFF_setHead( fbp, bpp, gx, gy, 0, NULL );
  268.     fwrite( fbp, 1, size, fp );
  269.  
  270.     fsize=0;
  271.     TIFF_setSaveFunc( put_file, get_data );
  272.     TIFF_saveImage( bpp, gx, gy, c, fbp, FILEBUF, gbp, dx, gy, cbp );
  273.  
  274.     if( c==1 )    {
  275.         size=0;
  276.     }
  277.     else    {
  278.         size=fsize;
  279.     }
  280.     if( clt )    {
  281.         pbpw=pbp;
  282.     }
  283.     else    {
  284.         pbpw=NULL;
  285.     }
  286.  
  287.     rewind( fp );
  288.     size=TIFF_setHead( fbp, bpp, gx, gy, size, pbpw );
  289.     fwrite( fbp, 1, size, fp );
  290.  
  291.     fclose( fp );
  292.     return( 0 );
  293. }
  294.  
  295.  
  296. /****************************************************************
  297. *    ファイルのパス作成                                                *
  298. ****************************************************************/
  299. void    make_path( char *fpath, char *path, char *file )    {
  300.     char    *fwp;
  301.  
  302.     if( path!=NULL )    {
  303.         strcpy( fpath, path );
  304.         if( (fwp=strrchr( fpath, '\\' ))!=NULL )    {
  305.             fwp++;
  306.         }
  307.         else if( (fwp=strchr( fpath, ':' ))!=NULL )    {
  308.             fwp++;
  309.         }
  310.         else    {
  311.             fwp=fpath;
  312.         }
  313.     }
  314.     else    {
  315.         fwp=fpath;
  316.     }
  317.     strcpy( fwp, file );
  318. }
  319.  
  320.  
  321. /****************************************************************
  322. *    TIFFファイルの選択ル-チン                                            *
  323. ****************************************************************/
  324. void    select_tiff( int num, char *path )    {
  325.     struct find_t        file;
  326.     struct dostime_t    time;
  327.     int                    x;
  328.     int                    t;
  329.     char                *tmenu;
  330.  
  331.     if( !_dos_findfirst( path, _A_NORMAL, &file ) )    {
  332.         if( num<1 || num>FILEMAX ) num=FILEMAX;
  333.         for( x=0;; )    {
  334.             strcpy( &fname[x*13], file.name );
  335.             x++;
  336.             if( x>=num ) break;
  337.             if( _dos_findnext( &file ) ) break;
  338.         }
  339.  
  340.         _dos_gettime( &time );
  341.         x=((time.hsecond
  342.             +100*(time.second
  343.             + 60*(time.minute
  344.             + 60* time.hour  )))/8) % x;
  345.         make_path( sfile, path, &fname[x*13] );
  346.  
  347.         if( (tmenu=getenv("TMENU"))!=NULL )    {
  348.             strcpy( dfile, tmenu );
  349.             t=strlen(dfile)-1;
  350.             if( dfile[t]=='\\' )    {
  351.                 dfile[t]='\0';
  352.             }
  353.         }
  354.         else    {
  355.             strcpy( dfile,"\0" );
  356.         }
  357.         strcat( dfile, "\\tmenu.tif" );
  358.  
  359.         printf( "ファイル展開 = %s ", sfile );
  360.         if( load_file( sfile )==0 )    {
  361.             printf( " -> %s \n", dfile );
  362.             save_file( dfile, 1 );
  363.         }
  364.     }
  365. }
  366.  
  367.  
  368. /****************************************************************
  369. *    TIFFファイルの圧縮ル-チン                                            *
  370. ****************************************************************/
  371. void    comp_tiff( char *path )    {
  372.     struct find_t        file;
  373.  
  374.     if( !_dos_findfirst( path, _A_NORMAL, &file ) )    {
  375.         while( 1 )    {
  376.             make_path( sfile, path, file.name );
  377.             if( load_file( sfile )==0 )    {
  378.                 printf( "ファイル圧縮 = %s \n", sfile );
  379.                 save_file( sfile, 5 );
  380.             }
  381.             if( _dos_findnext( &file ) ) break;
  382.         }
  383.     }
  384. }
  385.  
  386.  
  387. /****************************************************************
  388. *    画面からTIFFファイル作成ル-チン                                    *
  389. ****************************************************************/
  390. void    read_tiff( char *path, int pg, int md )    {
  391.     if( load_scrn( pg, md )==0 )    {
  392.         printf( "ファイル作成 = %s \n", path );
  393.         save_file( path, 5 );
  394.     }
  395. }
  396.  
  397.  
  398. /****************************************************************
  399. *    画面にTIFFファイル表示ル-チン                                        *
  400. ****************************************************************/
  401. void    disp_tiff( char *path )    {
  402.     struct find_t        file;
  403.  
  404.     if( !_dos_findfirst( path, _A_NORMAL, &file ) )    {
  405.         while( 1 )    {
  406.             make_path( sfile, path, file.name );
  407.             if( load_file( sfile )==0 )    {
  408.                 disp_data();
  409.                 while( _kbhit()==0 );
  410.                 while( _kbhit() )    {
  411.                     _getche();
  412.                 }
  413.             }
  414.             if( _dos_findnext( &file ) ) break;
  415.         }
  416.     }
  417. }
  418.  
  419.  
  420. /****************************************************************
  421. *    メインル-チン(コマンド分岐)                                            *
  422. ****************************************************************/
  423. void    main( int ac, char **av )    {
  424.     int        num;
  425.     int        pg;
  426.     int        md;
  427.  
  428.     if( (gwork=malloc( EgbWorkSize ))==NULL ) memerr();
  429.     if( (fname=malloc( FILEMAX*13 ))==NULL ) memerr();
  430.     if( (sfile=malloc( 256 ))==NULL ) memerr();
  431.     if( (dfile=malloc( 256 ))==NULL ) memerr();
  432.     if( (fbp=malloc( FILEBUF ))==NULL ) memerr();
  433.     if( (gbp=malloc( GRPHBUF ))==NULL ) memerr();
  434.     if( (pbp=malloc( PALTBUF ))==NULL ) memerr();
  435.     if( (cbp=malloc( COMPBUF ))==NULL ) memerr();
  436.  
  437.     if( ac<3 ) help();
  438.     if( *(av[1]+0)!='-' ) help();
  439.     switch( *(av[1]+1) )    {
  440.         case    's':
  441.         case    'S':
  442.             num=atoi( av[1]+2 );
  443.             select_tiff( num, av[2] );
  444.             break;
  445.  
  446.         case    'c':
  447.         case    'C':
  448.             comp_tiff( av[2] );
  449.             break;
  450.  
  451.         case    'g':
  452.         case    'G':
  453.             if( ac>3 )    {
  454.                 pg=atoi( av[3] );
  455.             }
  456.             else    {
  457.                 pg=0;
  458.             }
  459.             if( ac>4 )    {
  460.                 md=atoi( av[4] );
  461.             }
  462.             else    {
  463.                 md=1;
  464.             }
  465.             read_tiff( av[2], pg, md );
  466.             break;
  467.  
  468.         case    'd':
  469.         case    'D':
  470.             disp_tiff( av[2] );
  471.             break;
  472.  
  473.         default:
  474.             help();
  475.     }
  476.  
  477.     free( fname );
  478.     free( sfile );
  479.     free( dfile );
  480.     free( fbp );
  481.     free( gbp );
  482.     free( pbp );
  483.     free( cbp );
  484. }
  485.